<?php
namespace Category;

class Category {

	//清除包含对应字段的分类
	static public function clearCate($cate, $status = 'status', $value = 1) {

		$arr = array();
		foreach ($cate as $v) {
			if ($v[$status] != $value) {
				$arr[] = $v;
			}
		}
		return $arr;		
	}

	
	//一维数组
	static public function toLevel($cate,$pid = 0,$idName = 'id',$pidName = 'pid',$delimiter = '———', $level = 0) {
		$arr = array();
		foreach ($cate as $v) {
			if ($v[$pidName] == $pid) {
				$v['level'] = $level + 1;
				$v['delimiter'] = str_repeat($delimiter, $level);
				$arr[] = $v;
				$arr = array_merge($arr, self::toLevel($cate,$v[$idName],$idName,$pidName,$delimiter,$v['level']));
			}
		}
		return $arr;
	}
	//一维数组 树形结构
	static public function toLevelTree($cate,$pid = 0,$idName = 'id',$pidName = 'pid', $level = 0) {
		$arr = [];
		foreach ($cate as $v) {
			if ($v[$pidName] == $pid) {
				$v['level'] = $level + 1;
				$arr[] = $v;
				$arr = array_merge($arr, self::toLevelTree($cate,$v[$idName],$idName,$pidName,$v['level']));
			}
		}
		if ($level === 0) return self::formatPreffix($arr);
		return $arr;
	}

	protected  static function formatPreffix($arr){
        foreach ($arr as $k=>&$v){
            $str = "";
            if ($v['level'] > 2) {
                for ($i = 1; $i < $v['level'] - 1; $i++) {
                    $str .= "&emsp;│";
                }
            }
            if ($v['level'] != 1) {
                if (isset($arr[$k + 1]) && $arr[$k + 1]['level'] >= $arr[$k]['level']) {
                    $v['preffix'] = $str . "&emsp;├─ ";
                } else {
                    $v['preffix'] = $str . "&emsp;└─ ";
                }
            }else{
                $v['preffix']   =   '';
            }
        }
        return $arr;
    }




	//组成多维数组
	static public function toLayer($cate, $name = 'child', $pid = 0,$idName =   'id'){
		$arr    =   [];
		foreach ($cate as $v) {
			if ($v['pid'] == $pid) {
				$v[$name] = self::toLayer($cate, $name, $v[$idName],$idName);
				$arr[] = $v;
			}
		}
		return $arr;
	}


	//一维数组(同模型)(model = tablename相同)，删除其他模型的分类
	static public function getLevelOfModel($cate, $tablename = 'article') {
		$arr = array();
		foreach ($cate as $v) {
			if ($v['tablename'] == $tablename) {
				$arr[] = $v;
			}
		}
		return $arr;
	}

	//一维数组(同模型)(modelid)，删除其他模型的分类
	static public function getLevelOfModelId($cate, $modelid = 0) {

		$arr = array();
		foreach ($cate as $v) {
			if ($v['modelid'] == $modelid) {
				$arr[] = $v;
			}
		}

		return $arr;
		
	}

	//传递一个子分类ID返回他的所有父级分类
	static public function getParents($cate, $id,$idName = 'id',$pidName = 'pid') {
		$arr = array();
		foreach ($cate as $v) {
			if ($v[$idName] == $id) {
				$arr[] = $v;
				$arr = array_merge(self::getParents($cate, $v[$pidName],$idName,$pidName), $arr);
			}
		}
		return $arr;
	}

	//传递一个子分类ID返回他的同级分类
	static public function getSameCate($cate, $id,$idName = 'id',$pidName = 'pid') {
		$arr = array();
		$self = self::getSelf($cate, $id,$idName);
		if (empty($self)) {
			return $arr;
		}

		foreach ($cate as $v) {
			if ($v[$idName] == $self[$pidName]) {
				$arr[] = $v;
			}
		}
		return $arr;
	}



	//判断分类是否有子分类,返回false,true
	static public function hasChild($cate, $id,$idName = 'id',$pidName = 'pid') {
		$arr = false;
		foreach ($cate as $v) {
			if ($v[$pidName] == $id) {
				$arr = true;
				return $arr;
			}
		}

		return $arr;
	}

	//传递一个父级分类ID返回所有子分类ID
	/**
	*@param $cate   array
	*@param $pid    int
	*@param $flag   boolean
	**/
	static public function getChildsId($cate, $pid,$flag = false,$idName = 'id',$pidName = 'pid') {
		$arr = [];
        if ($flag){
            $arr[] = $pid;
        }
		foreach ($cate as $v) {
			if ($v[$pidName] == $pid) {
				$arr[] = $v[$idName];
				$arr = array_merge($arr , self::getChildsId($cate, $v[$idName],$flag,$idName));

			}
		}
		return $arr;
	}


	//传递一个父级分类ID返回所有子级分类
	static public function getChilds($cate, $pid,$idName = 'id',$pidName = 'pid',$level = 0) {
		$arr = array();
		foreach ($cate as $v) {
			if ($v[$pidName] == $pid) {
			    $v['level']     =   $level+1;
				$arr[] = $v;
				$arr = array_merge($arr, self::getChilds($cate, $v[$idName],$idName,$pidName,$v['level']));
			}
		}
		return $arr;
	}

	static public function getChild($cate,$pid,$pidName = 'pid'){
        $arr = [];
        foreach ($cate as $v) {
            if ($v[$pidName] == $pid) {
                $arr[] = $v;
           }
        }
        return $arr;
    }

	//传递一个分类ID返回该分类相当信息
	static public function getSelf($cate, $id,$idName = 'id',$pidName = 'pid') {
		$arr = array();
		foreach ($cate as $v) {
			if ($v[$idName] == $id) {
				$arr = $v;
				return $arr;
			}
		}
		return $arr;
	}


}


?>